/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kasgroup2;

/**
 *
 * @author beha
 */
public class UIHotelOverview extends javax.swing.JPanel {

    private TableManager tmh = null;
    
    /**
     * Creates new form UIHotelOverview
     */
    public UIHotelOverview() {
        initComponents();
        tmh = new TableManager(this.tblHotels);
        fillHotelsTable();
    }

    private void fillHotelsTable() {
        tmh.clear();
        for(Hotel h : KASGroup2.getCoreHandlerInstance().getHotelList()) {
            tmh.addRow(new Object[] { h.getName(), h.getAddress(), h.getRoomCount() });
        }
    }
    
    private void openHotelFrame(Hotel p_Hotel) {
        UICreateHotelFrame uichf = new UICreateHotelFrame(p_Hotel);
        uichf.setVisible(true);
        uichf.pack();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblHotels = new javax.swing.JTable();
        btnAddHotel = new javax.swing.JButton();
        btnDeleteHotel = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Hotel Overview"));

        tblHotels.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Name", "Address", "Rooms"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblHotels.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblHotelsMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblHotels);
        tblHotels.getColumnModel().getColumn(0).setResizable(false);
        tblHotels.getColumnModel().getColumn(0).setPreferredWidth(75);
        tblHotels.getColumnModel().getColumn(1).setResizable(false);
        tblHotels.getColumnModel().getColumn(1).setPreferredWidth(200);
        tblHotels.getColumnModel().getColumn(2).setResizable(false);
        tblHotels.getColumnModel().getColumn(2).setPreferredWidth(75);

        btnAddHotel.setText("Add");
        btnAddHotel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddHotelActionPerformed(evt);
            }
        });

        btnDeleteHotel.setText("Delete");
        btnDeleteHotel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteHotelActionPerformed(evt);
            }
        });

        btnRefresh.setText("Refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 655, Short.MAX_VALUE)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(btnRefresh)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnAddHotel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnDeleteHotel)
                        .add(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnAddHotel)
                    .add(btnDeleteHotel)
                    .add(btnRefresh))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddHotelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddHotelActionPerformed
        openHotelFrame(null);
        fillHotelsTable();
    }//GEN-LAST:event_btnAddHotelActionPerformed

    private void btnDeleteHotelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteHotelActionPerformed
        KASGroup2.getCoreHandlerInstance().removeHotel((String)tmh.getCellValueForSelectedRow(0));
        fillHotelsTable();
    }//GEN-LAST:event_btnDeleteHotelActionPerformed

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        fillHotelsTable();
        //ObjectSerializer os = new ObjectSerializer();
        //os.SerializeHotels(KASGroup2.getCoreHandlerInstance().getHotelList());
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void tblHotelsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblHotelsMouseClicked
        if(evt.getClickCount() == 2) {
            int index = tmh.getSelectedRowIndex();
            Hotel hotel = KASGroup2.getCoreHandlerInstance().getHotel(index);
            openHotelFrame(hotel);
        }
    }//GEN-LAST:event_tblHotelsMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddHotel;
    private javax.swing.JButton btnDeleteHotel;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblHotels;
    // End of variables declaration//GEN-END:variables
}
